/*
 * @Author: actor
 * @Date: 2023-11-09 17:36:53
 * @email: 395593455@qq.com
 * @LastEditors: actor
 * @LastEditTime: 2023-12-21 10:44:14
 * @FilePath: \i-hos-app\shared\emr\src\store\emrPageShare.ts
 * @Description: 电子病历共享页面的配置数据
 */
// 引入pinia
import { defineStore } from 'pinia'
import { Names } from './Name'

// 配置类型约束
import { IPageShare } from '../interface/pageShare'

// 页面数据仓库
export const useEmrPageShare = defineStore(Names.PageShare, {
  state: (): IPageShare => {
    return {
      // 当前选中的分类菜单
      activetreeList: [],
      // 归属机构
      organTreeList: [],
      // 分类对应表格
      tableList: [],
      pageNum: 1,
      pageSize: 20,
      totalPage: 0,
    }
  },
  actions: {
    // 获取分类表格的数据
    fetchCategoryTable($fetch: any, flag?: any): void {
      // 设置传递的参数
      let params: any = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        ids: [],
        emrKnowledgeBaseMainCode: !!flag && flag.code ? flag.code : '',
        keyword: !!flag && flag.keyword ? flag.keyword : '',
      }

      // 查找对应所有的分类id(所有子级)
      params.ids.push(this.activetreeList.emrKnowledgeBaseCategoryId)
      function getId(data: any, arr: number[]) {
        data.forEach((ele: any) => {
          arr.push(ele.emrKnowledgeBaseCategoryId)
          // 判断有子元素,并且子元素的长度大于0就再次调用自身
          if (ele.childList && ele.childList.length > 0) {
            getId(ele.childList, arr)
          }
        })
      }
      if (this.activetreeList && this.activetreeList?.childList && this.activetreeList.childList.length > 0) {
        getId(this.activetreeList.childList, params.ids)
      }

      // 获取去分类表格数据，默认返回第一个大类对应的列表信息
      const categoryTableListUrl: string = APP_GLOBAL.API['EMR']['category-table-list']
      $fetch.$http.requestJSON('post', categoryTableListUrl, params).then((res: any) => {
        if (!!res.data) {
          this.tableList = res.data.records
          this.totalPage = Number(res.data.total)
        }
      })
    },
    // 获取病历模板分类表格数据
    fetchOrganizationTable($fetch: any, flag?: any): void {
      let params: any = {
        deptId: !!flag && !!flag.deptId ? flag.deptId : '',
        docTempName: !!flag && !!flag.docTempName ? flag.docTempName : '',
        docTempStatus: !!flag && !!flag.docTempStatus ? flag.docTempStatus : '',
        docTempType: !!flag && !!flag.dictItemCode ? flag.dictItemCode : '',
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      }
      // 获取病历模板列表
      const emrTemplateTistUrl: string = APP_GLOBAL.API['EMR']['emr-template-list']
      $fetch.$http.requestJSON('post', emrTemplateTistUrl, params).then((res: any) => {
        if (!!res.data) {
          this.tableList = res.data.records
          this.totalPage = Number(res.data.total)
        }
      })
    },
  },
})
